package com.valdemar.cookerybook.dao;

import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Hashtable;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.valdemar.cookerybook.model.Ingredient;

public enum IngredientDAO
{
	instance;

	public List<Ingredient> listAllIngredients(int languageId)
	{
		EntityManager em = EMFService.get().createEntityManager();
		// Read the existing entries
		Query q = em.createQuery("select m from Ingredient m");
		
		List<Ingredient> ingredients = q.getResultList();
		
		Hashtable<String, String> translations = TranslationDao.instance.getTranslations(Ingredient.class.getName(), languageId);
		
		mergeTranslationWithIngredient(ingredients, translations);
		
		return ingredients;
	}

	private void mergeTranslationWithIngredient(List<Ingredient> ingredients, Hashtable<String, String> translations)
	{
		for (Ingredient ingredient : ingredients)
		{
			ingredient.setTranslate(translations.get(ingredient.getCode()));
		}
		
	}

	public void add(String ingredientCode, int langID, String translation)
	{
		synchronized (this)
		{
			EntityManager em = EMFService.get().createEntityManager();
			Ingredient ingredient = new Ingredient(ingredientCode);
			em.persist(ingredient);
			em.close();

			TranslationDao.instance.add(Ingredient.class.getName(), ingredientCode, translation, langID);
		}
	}

	// public List<Todo> getTodos(String userId) {
	// EntityManager em = EMFService.get().createEntityManager();
	// Query q = em
	// .createQuery("select t from Todo t where t.author = :userId");
	// q.setParameter("userId", userId);
	// List<Todo> todos = q.getResultList();
	// return todos;
	// }

	public Ingredient getIngredient(long id)
	{
		EntityManager em = EMFService.get().createEntityManager();
		Ingredient ingredient = null;
		try
		{
			ingredient = em.find(Ingredient.class, id);
		}
		finally
		{
			em.close();
		}
		return ingredient;
	}

	public Ingredient getIngredient(String code, int languageId)
	{
		EntityManager em = EMFService.get().createEntityManager();
		Ingredient ingredient = null;
		try
		{

			Query q = em.createQuery("select i from Ingredient i where i.code = :code");
			q.setParameter("code", code);
			ingredient = (Ingredient) q.getSingleResult();

			if (ingredient == null)
				return null;
			String tr = TranslationDao.instance.getTranslation(Ingredient.class.getName(), code, languageId);

			ingredient.setTranslate(tr);
		}
		finally
		{
			em.close();
		}
		return ingredient;
	}

	public boolean remove(long id)
	{
		EntityManager em = EMFService.get().createEntityManager();
		try
		{
			Ingredient ingredient = em.find(Ingredient.class, id);
			if (ingredient == null)
				return false;
			em.remove(ingredient);
		}
		finally
		{
			em.close();
		}

		return true;
	}

	public void addBulk(String ingredients, int languageId)
	{
		// TODO Auto-generated method stub
		Gson gson = new Gson();
		
		Type collectionType = new TypeToken<Collection<Ingredient>>(){}.getType();
		Collection<Ingredient> ints2 = gson.fromJson(ingredients, collectionType);
		
	}

	public int getCount()
	{
		EntityManager em = EMFService.get().createEntityManager();
		// Read the existing entries
		Query q = em.createQuery("select m from Ingredient m");
		
		List<Ingredient> ingredients = q.getResultList();
		
		return ingredients.size();
	}

}